<?php

namespace pmvc\template;

use pmvc\ioc\ObjectFactory;
use pmvc\ioc\InitializingObject;
use pmvc\log\Log;
use pmvc\util\ClassUtil;

class SmartyTemplateEngine
	implements TemplateEngine,
	InitializingObject {

    private $compileDir;
    private $configDir;
    private $cacheDir;
    private $useCache;
    private $templateDir;
	
	public function initialize(ObjectFactory $objectFactory) {
		
		// bring in smarty
		ClassUtil::requireFile("/smarty/Smarty.class.php", TRUE);
    	
		// create needed directories
		if (!file_exists($this->compileDir) 
			&& !mkdir($this->compileDir, 755, TRUE)) {
			Log::error("Unable to create compile dir: {0}", $this->compileDir);
		}
		if (!file_exists($this->configDir) 
			&& !mkdir($this->configDir, 755, TRUE)) {
			Log::error("Unable to create config dir: {0}", $this->configDir);
		}
		if (!file_exists($this->cacheDir) 
			&& !mkdir($this->cacheDir, 755, TRUE)) {
			Log::error("Unable to create cache dir: {0}", $this->cacheDir);
		}
	}

	public function getTemplate($name) {

        // create smarty
        $smarty = new \Smarty();
        
        // configure
        $smarty->template_dir 	= $this->templateDir;
        $smarty->compile_dir  	= $this->compileDir;
        $smarty->config_dir  	= $this->configDir;
        $smarty->cache_dir    	= $this->cacheDir;
        $smarty->caching		= strtolower($this->useCache)=="true";
        $smarty->use_sub_dirs	= TRUE;
 
        // return the template
		return new SmartyTemplate($smarty, $name);
	}

	public function setCompileDir($compileDir) {
		$this->compileDir = $compileDir;
	}

    public function setConfigDir($configDir) {
		$this->configDir = $configDir;
	}
    
    public function setCacheDir($cacheDir) {
		$this->cacheDir = $cacheDir;
	}
    
    public function setUseCache($useCache) {
		$this->useCache = $useCache;
	}
    
    public function setTemplateDir($templateDir) {
		$this->templateDir = $templateDir;
	}
    
}

?>